{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "cost is[[14.00000083]]\n",
      "dz is[[ 9.99999168e-01 -1.26565425e-14]]\n",
      "b is[[0.49999958]]\n",
      "w is[[1.99999917]\n",
      " [3.99999834]\n",
      " [5.99999751]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "def plus_fun(w, x, b):\n",
    "    return np.dot(w.T, x) + b \n",
    "\n",
    "def sigmod(x):\n",
    "    return 1/(1+np.exp(-x))\n",
    "\n",
    "def cost_fun(y, a):\n",
    "#     print(np.dot(y, np.log(a).T))\n",
    "    return -(np.dot(y, np.log(a).T) + np.dot((1-y), np.log(1-a).T))\n",
    "\n",
    "def dz(y, a):\n",
    "    return a - y\n",
    "\n",
    "def run():\n",
    "    W = np.array([\n",
    "        [1.0],\n",
    "        [2.0],\n",
    "        [3.0]\n",
    "    ])\n",
    "    X = np.array([\n",
    "        [1.0, 4.0],\n",
    "        [2.0, 5.0],\n",
    "        [3.0, 6.0],\n",
    "    ])\n",
    "    b = np.zeros((1, 1))\n",
    "    \n",
    "    y = np.array([\n",
    "        [0.0, 1.0],\n",
    "    ])\n",
    "    \n",
    "#     print(np.dot(W.T, X))\n",
    "#     print(b)\n",
    "    pf = plus_fun(W, X, b)\n",
    "    assert(pf.shape == (1, 2))\n",
    "    \n",
    "    sd = sigmod(pf)\n",
    "    assert(sd.shape == (1, 2))\n",
    "    \n",
    "    cf = cost_fun(y, sd)\n",
    "    print('cost is' + str(cf))\n",
    "    assert(cf.shape == (1, 1))\n",
    "    \n",
    "    _dz = dz(y, sd)\n",
    "    assert(_dz.shape == (1, 2))\n",
    "    print('dz is' + str(_dz))\n",
    "    \n",
    "    b += np.sum(_dz)/_dz.shape[1]\n",
    "    print('b is' + str(b))\n",
    "    \n",
    "#     W = W.astype('float64')\n",
    "    W += np.sum(np.dot(X, _dz), axis=1).reshape(3, 1)\n",
    "    print('w is' + str(W))\n",
    "    \n",
    "    \n",
    "\n",
    "run()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
